package com.bioscript.standalone.imps;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;

import com.bioscript.standalone.IO.FileReader;
import com.bioscript.standalone.dataStructures.Contig;
import com.bioscript.standalone.dataStructures.Generation;
import com.bioscript.standalone.dataStructures.Project;
import com.bioscript.standalone.dataStructures.Range;

public class ContigVisualisingScript extends Visualiser{

	public static void main(String[] args) throws ParserConfigurationException
	{
		ContigVisualisingScript contigVisualisingScript = new ContigVisualisingScript();
		ArrayList<ArrayList<Contig>> ranges = new ArrayList<ArrayList<Contig>>();
		
		int seqLength = Integer.parseInt(args[0]);
		String projectFolder = args[1];
		String vxmlFile = args[2];
		String title = args[3];
		String projectDescriptionFile = args[4];
		String contigsFileName = args[5];
		
		ProjectDescription description = (new ProjectDescription()).parse(projectDescriptionFile);
		Project prj = description.getLst().get(0);
		
		List<Generation> gens = prj.getGenerations();
		
		String[] names = new String[gens.size()];
		
		int index = 0;
		for (Generation gen : gens)
		{
			ranges.add(FileReader.readProcessedContigs(projectFolder + 
					"/" + gen.getName() + "/" + contigsFileName));
			names[index] = gen.getName();
			index++;
		}
		contigVisualisingScript.makeVisualisation(ranges, seqLength, names, vxmlFile, title);
	}
	
	public void makeVisualisation(ArrayList<ArrayList<Contig>> ranges,int seqLength,String[] names, 
			String vXml, String title) 
	throws ParserConfigurationException
	{
		initialise(String.valueOf(seqLength),names, null);
		makeFeatureSlots(names.length);
		int index = 0;
		for(ArrayList<Contig> range : ranges)
		{
			for(Contig contig : range)
			{
				Range r = contig.getMapping();
				if (r.getLowerLimit() < 0)
				{
					r.setLowerLimit(seqLength + r.getLowerLimit());
				}
				String mouseOver = "Start = " + String.valueOf(r.getLowerLimit())
				+ ", stop = " + String.valueOf(r.getUpperLimit());
				Element feature = prepareXMLFile.createFeature("directStrand" + index, colors[index], 
						"clockwise-arrow", 
						contig.getContig().getName(), null,mouseOver,null,null,null);
				prepareXMLFile.createFeatureRange(feature, String.valueOf(r.getLowerLimit()), 
						String.valueOf(r.getUpperLimit()), null);
			}
			index++;
		}
		prepareXMLFile.writeToFile(vXml);
	}
}
